package com.example.demo.register;

import com.example.demo.config.springsecurity.UserInfo;
import com.example.demo.constants.DemoConstants;
import com.example.demo.domain.Response;
import com.example.demo.register.captcha.CaptchaService;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.StringRedisTemplate;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpServletResponse;
import java.io.IOException;

@Slf4j
@RestController
public class RegisterController {
    @Autowired
    UserInfoService userInfoService;
    @Autowired
    SendCodeService sendCodeService;
    @Autowired
    StringRedisTemplate redisTemplate;
    @Autowired
    CaptchaService captchaService;

    @RequestMapping(value = "/api/auth/send-code", method = RequestMethod.GET)
    public Response sendCode(String mail) {
        log.info("send code...");
        try {
            sendCodeService.send(mail);
        } catch (Exception e) {
            return Response.fail();
        }
        return Response.success();
    }

    @RequestMapping(value = "/api/register-action", method = RequestMethod.POST)
    public Response register(@Validated RegisterForm registerForm) {
        validationCode(registerForm);
        UserInfo userInfo = new UserInfo();
        BeanUtils.copyProperties(registerForm, userInfo);
        boolean save = userInfoService.save(userInfo);
        if (save) {
            return Response.success();
        } else {
            return Response.fail();
        }
    }

    private void validationCode(RegisterForm registerForm) {
        if (StringUtils.isEmpty(registerForm.getCode()) && StringUtils.isEmpty(registerForm.getValidCode())) {
            throw new RuntimeException("邮箱验证码和验证码必填一个");
        }
        String redisCode = redisTemplate.opsForValue().get(DemoConstants.REDIS_KEY_CODE_EMAIL + registerForm.getMail());
        if (StringUtils.isNotEmpty(registerForm.getValidCode()) && !registerForm.getValidCode().equals(redisCode)) {
            throw new RuntimeException("邮箱验证码错误");
        }
        String commonCode = redisTemplate.opsForValue().get(DemoConstants.REDIS_KEY_CODE_COMMON + registerForm.getUuid());
        if (StringUtils.isNotEmpty(registerForm.getValidCode()) && !registerForm.getValidCode().equals(redisCode)) {
            throw new RuntimeException("验证码错误");
        }
    }

    @RequestMapping("/api/common-code")
    public Response commonCode(HttpServletResponse response) {
        String uuid = "";
        try {
            uuid = captchaService.generateCode(response);
        } catch (IOException e) {
            throw new RuntimeException(e.getMessage());
        }
        return Response.success(uuid);
    }

}
